﻿using DG.Tweening;
using System;
using UnityEngine;

namespace CGF
{
    public class TextAnimation : MonoBehaviour
    {
        public bool playOnAwake = true;
        public float txtInterval = 0.1f;
        public float txtDuration = 0.3f;

        public void Start()
        {
            if (playOnAwake)
            {
                Play();
            }
        }

        public async void Play()
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                var child = transform.GetChild(i);
                child.DOAlpha(0);
                child.localScale = Vector3.one * 3;
            }


            for (int i = 0; i < transform.childCount; i++)
            {
                var child = transform.GetChild(i);
                child.DOAlpha(1, txtDuration);
                child.DOScale(1, txtDuration);
                await CTask.Wait(txtInterval);
                if (!enabled)
                {
                    return;
                }
            }

            await CTask.Wait(3);

            if (enabled)
            {
                Play();
            }
        }

    }
}
